# 强化学习环境类
import numpy as np
from apps.fmcw.conf.app_config import AppConfig as AF
from apps.fmcw.core.ira_observe import IraObserve
from apps.fmcw.core.ira_state import IraState
from apps.fmcw.core.fmcw_radar import FmcwRadar as FR
from apps.fmcw.core.ira_target import IraTarget as TGT
from apps.fmcw.core.ira_action import IraAction

class IraEnvironment(object):
    def __init__(self):
        self.name = 'apps.fmcw.core.ira_environment.IraEnvironment'
        self.steps = 0

    def setup(self) -> IraObserve:
        self.obs = IraObserve()
        self.state = IraState()
        FR.setup()
        return self.obs, self.state

    def step(self) -> IraObserve:
        AF.times = np.linspace(AF.t_sys, AF.t_sys + AF.T * AF.numChirps, AF.N_CPI)
        # t_onePulse = np.arange(AF.t_sys, AF.t_sys + AF.dt * AF.numADC, AF.dt)[:AF.numADC]
        t_onePulse = np.arange(0, AF.dt * AF.numADC, AF.dt)[:AF.numADC]
        # delta = (AF.T * AF.numChirps) / (AF.N_CPI - 1)
        AF.t_sys += AF.T * AF.numChirps
        # 更新目标位置
        tar1_loc, tar2_loc, ranges_, velocitys, thetas = TGT.step(AF.times)
        # 生成信号及回波信号，返回雷达立方体数据
        RDC = FR.step(AF.times, t_onePulse)
        self.obs.RDC = RDC
        # FR.draw_signals() # 绘制发射和回波信号波形图
        self.steps += 1
        # if self.steps > 5:
        #     self.state.done = True
        return self.obs, self.state
    
    def execute(self, action:IraAction) -> None:
        if action.dt_action[0] == 888:
            self.state.done = True
        print(f'execute action...')